import java.util.Arrays;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2021/11/18
 * \* Time: 12:16
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class MyArrayList {

    //1.定义顺序表的两个属性
    public int[] elem;
    public int useSize;

    //2.通过构造方法初始化
    public MyArrayList() {
        this.elem = new int[10];
        this.useSize = 0;
    }

    // 打印顺序表
    public void display() {
        for (int i=0;i<this.useSize;i++){
            System.out.print(this.elem[i]+" ");
        }
    }

    private boolean isFull(){
        if (this.useSize==this.elem.length){
            return true;
        }
        return false;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(isFull()){
            Arrays.copyOf(this.elem,2*this.elem.length);
        }
        if (pos<0||pos>useSize) {
            System.out.println("位置不合法");
            return;
        }
        for(int i=this.useSize-1;i>=pos;i--){
            this.elem[i+1]=this.elem[i];
        }
        this.elem[pos]=data;
        this.useSize++;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for(int i=0;i<this.useSize;i++){
            if(this.elem[i]==toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的  位置
    public int search(int toFind) {
        for (int i=0;i<this.useSize;i++){
            if(this.elem[i]==toFind){
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if (this.useSize!=0){
            if(pos<0||pos>useSize){
                return -1;
            }
        }
        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {

    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        int index=search(toRemove);
        if(index==-1){
            System.out.println("没有你需要删除的数据");
            return;
        }
        for (int i=index;i<useSize-1;i++){
            this.elem[i]=this.elem[i+1];
        }
        this.useSize--;
    }
    // 获取顺序表长度
    public int size() {
        return this.useSize;
    }
    // 清空顺序表
    public void clear() {
        this.useSize=0;
    }


}
